package com.cn.xiaonuo.slbfinances.slbagents.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.tools.RestTemplateUtil;
import com.cn.xiaonuo.core.exception.ServiceException;

import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slbagents.entity.SlUserData;
import com.cn.xiaonuo.slbfinances.slbagents.enums.SlXsProxyDataExceptionEnum;
import com.cn.xiaonuo.slbfinances.slbagents.mapper.UserAgentMapper;
import com.cn.xiaonuo.slbfinances.slbagents.service.UserAgentService;
import com.cn.xiaonuo.tcrfinance.tcragents.utils.AgentCst;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
public class UserAgentServiceImpl extends ServiceImpl<UserAgentMapper, SlUserData> implements UserAgentService {

    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private Environment env;
    /**
     * 获取平台相关协议
     *
     * @author luyangyang
     * @date 2021-11-03 17:43:18
     */
    @DataSource(name = "hpc_agent_db")
    @Override
    public SlUserData queryOne(Integer userId) {
       /* QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(new SlUserData() {{
            setUserId(Integer.valueOf(userId));
        }});
        SlUserData SlUserData = userAgentMapper.selectOne(queryWrapper);//获取代理数据*/
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userId",userId);
        String jsonString = JSON.toJSONString(jsonObject);
        String url=env.getProperty("https.jybAgent") + AgentCst.financialSuperiorDetails;
        String respose = restTemplateUtil.postString(url, null, jsonString, null);
        JSONObject data = JSONObject.parseObject(respose);
        JSONObject jsonObject1= data.getJSONObject("data");
        SlUserData slUserData = new SlUserData();
        if (ObjectUtils.isNotEmpty(jsonObject1)){
             slUserData = jsonObject1.toJavaObject(SlUserData.class);
        }else {
            slUserData.setAgent(0);
        }
        return slUserData;
    }

}
